from gaev import utils
from gaev.actions.url import get_action_url 

from data import models

ev2capt={'ok':'ok', 'discovery':'new', 'wrong': 'bad', 'fail': 'fail', 'nothing': 'none'}
def evaluate_result(result, problem=None, problem_status=None):
	if not problem_status:
		problem_status=problem.status
	result_type=result.resultType
	query=models.ResultEvaluation.all()
	query.filter('resultType = ', result_type)
	query.filter('problemStatus = ', problem_status)
	evaluation=query.get()
	if evaluation:
		res={'ok':evaluation.ok, 'wrong':evaluation.wrong, 'discovery':evaluation.discovery, 'fail': evaluation.fail}
	else:
		res={'ok':False, 'wrong':False, 'discovery':False, 'fail': False}
	if res['discovery']:
		res['type']='discovery'
	elif res['wrong']:
		res['type']='wrong'
	elif res['fail']:
		res['type']='fail'
	elif res['ok']:
		res['type']='ok'
	else:
		res['type']='nothing'
	res['caption']=ev2capt[res['type']]
	return res
	
def get_results_query(problem, prover):
	query=models.Result.all()
	query.filter('problem = ', problem)
	query.filter('prover = ', prover)
	query.order('-added')
	return query

def get_problem_prover_table(problems, provers):
	#we use dictionaries instead of plain lists so it would be easier to access the values
	#from templates
	rows={}
	for problem in problems:
		cols={}
		problem_result_count=0
		for prover in provers:
			results_data=[]
			results=get_results_query(problem, prover)
			for result in results:
				res_ev=evaluate_result(result, problem)
				result_data={
							'type': res_ev['type'], 
							'time': result.time,
							'url': get_action_url('details', result),
							'caption': res_ev['caption'] 
							}
				results_data.append(result_data)
			cols[prover.short]=results_data
			problem_result_count+=len(results_data)
			
		cols['result_count']=problem_result_count
		rows[problem.short]=cols
	return rows;

def get_prover_problem_table(problems, provers):
	#we use dictionaries instead of plain lists so it would be easier to access the values
	#from templates
	from gaev import actions
	rows={}
	for prover in provers:
		cols={}
		for problem in problems:
			results_data=[]
			results=get_results_query(problem, prover)
			for result in results:
				res_ev=evaluate_result(result, problem)
				result_data={
							'type': res_ev['type'], 
							'time': result.time,
							'url': get_action_url('details', result),
							'caption': ev2capt[res_ev['type']] 
							}
				results_data.append(result_data)
			cols[problem.short]=results_data
		rows[prover.short]=cols
	return rows;

def get_provers_data(provers, context):
	provers_data=[]
	for prover in provers:
		prover_capt=utils.humanize_reference(prover, context=context)
		prover_data={'caption': prover_capt, 'short':prover.short}
		provers_data.append(prover_data)
	return provers_data

def get_problems_data(problems, context):
	problems_data=[]
	for problem in problems:
		problem_capt=utils.humanize_reference(problem, context=context)
		problem_data={'caption': problem_capt, 'short':problem.short, 'status': problem.status.abbr}
		problems_data.append(problem_data)
	return problems_data

